<?php
!defined('IN_HDWIKI') && exit('Access Denied');

class control extends base{
	function control(& $get,& $post){
		$this->base( & $get,& $post);
		$this->load('user');
		$this->load("doc");
		$this->load("category");
		$this->load("comment");
	}
	function doview(){
		if(!is_numeric($this->get[2])){
			$this->message($this->view->lang['parameterError'],'index.php',0);
		}
		$doc=$_ENV['doc']->get_doc($this->get[2]);
		if(!(bool)$doc){
			$this->message($this->view->lang['docNotExist'],'index.php',0);
		}elseif($doc['visible']=='0'&&!$this->checkable('admin_doc-audit')){
			$this->message($this->view->lang['viewDocTip4'],'index.php',0);
		}elseif(@$this->get[3]=="locked"&&$doc['locked']==1){
			$this->view->assign('locked','1');
		}
		$doc['time']=$this->date($doc['time']);
		
		$page = max(1, intval($this->get[3]));
		$num = isset($this->setting['list_prepage'])?$this->setting['list_prepage']:20;
		$start_limit = ($page - 1) * $num;
		
		$comments=$_ENV['comment']->get_comments($this->get[2],$start_limit,$num);
		$comments = array_pad($comments,$num,'');
		$departstr=$this->multi($doc['comments'], $num, $page,"comment-view-".$this->get[2]);

		$category=$_ENV['category']->get_category($doc['cid']);
		$navigation=unserialize($category['navigation']);

		$this->view->assign('doc',$doc);
		$page=$doc['comments']<=$num?0:$page;
		$this->view->assign('page',$page);
		$this->view->assign('num',$num);
		
		$this->view->assign('audit_edit',$this->checkable('comment-edit'));
		$this->view->assign('audit_delete',$this->checkable('comment-delete'));
		$this->view->assign('audit_add',$this->checkable('comment-add'));
		
		$this->view->assign('ausubmit',$this->checkable('comment-add'));
		$this->view->assign('navigation',$navigation);
		
		$this->view->assign("departstr",$departstr);
		$this->view->assign('comments',$comments);
		$this->view->display('viewcomment');
	}
	function dodelete(){
		$id=$this->post['id'];
		$did=$this->post['did'];
		$page=$this->post['page'];

		if($_ENV["comment"]->remove_comment_by_id($id))
			$_ENV['doc']->update_field('comments',-1,$did,0);
			$doc=$_ENV['doc']->get_doc($did);
			$num = isset($this->setting['list_prepage'])?$this->setting['list_prepage']:20;
			$start_limit = ($page - 1) * $num;
			$comments=$_ENV['comment']->get_comments($did,$start_limit,$num);
			$comments = array_pad($comments,$num,'');
			$departstr=$this->multi($doc['comments'], $num, $page,"comment-view-$did");
			$message="<span id=\"page\" style=\"display:none\">{$page}</span>
			<ol class=\"ol wid680\">";
			foreach($comments as $key=>$comment){
				if($comment!==''){
					$message.="
						<li id=\"li_{$key}\" >
							<span id=\"c_{$comment['id']}\">{$comment[comment]}</span>
							<span class=\"block\">
								<label class=\"l\">".$this->view->lang['commentUser']."<a href=\"{$this->setting['seo_prefix']}user-space-{$comment['authorid']}{$setting['seo_suffix']}\" >{$comment['author']}</a>&nbsp;&nbsp;</label>
									<label class=\"l\">".$this->view->lang['commentTime']."<span class=\"gray\">{$comment['time']}</span></label>";
										if($this->checkable('comment-edit')){
											$message.="<a href=\"javascript:void(0)\"  onclick=\"del_comment({$comment['id']});\" class=\"r\">".$this->view->lang['commentDel']."</a>
											<a href=\"javascript:void(0)\"  onclick=\"edit_comment({$comment['id']});\" class=\"r bor-gray\">".$this->view->lang['commentEdit']."</a>";
										}
							$message.="</span>
						</li>";
				}else{
					$message.="<li id=\"li_{$key}\" style=\"display:none\"></li>";
				}
			}
			$message.="</ol>";
			if($departstr){
				$message.="
				<div id=\"fenye\" style=\"float:left;width:690px;margin:20px; display:inline;\">
				{$departstr}
				</div>";
			}
		$this->message($message,'',2);
	}
	function doadd(){
		$did=@$this->get[2];
		if(empty($did) || !isset($this->post['comment'])){
			$this->message(0,'',2);
		}
		$c_class=$this->post['c_class'];
		$comment=string::hiconv($this->post['comment']);
		$comment=string::stripscript($_ENV['doc']->replace_danger_word($comment));
		if(empty($comment)){
			$this->message(0,'',2);
		}elseif(strlen($comment)>200){
			$comment=string::substring($comment,0,200);
		}
		$id=$_ENV['comment']->add_comment($did,$comment);
		if($id)
			$_ENV['user']->add_credit($this->user['uid'],'user-comment',$this->setting['credit_comment']);
			$_ENV['doc']->update_field('comments',1,$did,0);
		if($id){
			if($c_class){
				$page=1;
				$doc=$_ENV['doc']->get_doc($did);
				$num = isset($this->setting['list_prepage'])?$this->setting['list_prepage']:20;
				$start_limit = ($page - 1) * $num;
				
				$comments=$_ENV['comment']->get_comments($did,$start_limit,$num);
				$comments = array_pad($comments,$num,'');				
				$departstr=$this->multi($doc['comments'], $num, $page,"comment-view-$did");
				
				$message="<span id=\"page\" style=\"display:none\">{$page}</span>
				<ol class=\"ol wid680\">";
				foreach($comments as $key=>$comment){
					if($comment!==''){
						$message.="
							<li id=\"li_{$key}\" >
								<span id=\"c_{$comment['id']}\">{$comment[comment]}</span>
								<span class=\"block\">
									<label class=\"l\">".$this->view->lang['commentUser']."<a href=\"".$this->setting['seo_prefix']."user-space-{$comment['authorid']}{$setting['seo_suffix']}\" >{$comment['author']}</a>&nbsp;&nbsp;</label>
										<label class=\"l\">".$this->view->lang['commentTime']."<span class=\"gray\">{$comment['time']}</span></label>";
											if($this->checkable('comment-edit')){
										$message.="<a href=\"javascript:void(0)\"  onclick=\"del_comment({$comment['id']});\" class=\"r\">".$this->view->lang['commentDel']."</a>
										<a href=\"javascript:void(0)\"  onclick=\"edit_comment({$comment['id']});\" class=\"r bor-gray\">".$this->view->lang['commentEdit']."</a>";
										}
								$message.="</span>
							</li>";
					}else{
						$message.="<li id=\"li_{$key}\" style=\"display:none\"></li>";
					}
				}
				$message.="</ol>";
				if($departstr){
					$message.="
					<div id=\"fenye\" style=\"float:left;width:690px;margin:20px; display:inline;\">
					{$departstr}
					</div>";
				}
			}else{
				$message="<p id='c_".$id."'>".stripslashes($comment)."</p>
				<span class=\"block\">
					<label class=\"l\">".$this->view->lang['commentUser']."<a href=\"{$this->setting['seo_prefix']}user-space-".$this->user['uid']."{$this->setting['seo_suffix']}\" >{$this->user['username']}</a>&nbsp;&nbsp;</label>
					<label class=\"l\">".$this->view->lang['commentTime']."<span class=\"gray\">".$this->date($this->time)."</span></label>";
						if($this->checkable('comment-edit')){
						$message.="<a href=\"javascript:void(0)\"  onclick=\"del_comment($id);\" class=\"r\">".$this->view->lang['commentDel']."</a>
						<a href=\"javascript:void(0)\"  onclick=\"edit_comment($id);\" class=\"r bor-gray\">".$this->view->lang['commentEdit']."</a>";
						}
				$message.="</span>";
			}
			$this->message($message,'',2);
		}else{
			$this->message(0,'',2);
		}
	}
	function doedit(){
		$id=$this->post['id'];
		if(empty($id)){
			$this->message($this->view->lang['commentError'],'',2);
		}
		$comment=string::hiconv(trim($this->post['comment']));
		$comment=string::stripscript($_ENV['doc']->replace_danger_word($comment));
		$_ENV["comment"]->edit_comment_by_id($id,$comment);
		$this->message(1,'',2);
	}
}
?>